package com.ymatou.userservice.testcase;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.auth.parameter.LoginAuthBean;
import com.ymatou.auth.service.LoginAuthCall;
import com.ymatou.userservice.parameter.SetThirdPartyUserLoginBillsDtoBean;
import com.ymatou.userservice.parameter.WeiXinUserRequestDtoBean;
import com.ymatou.userservice.service.SetThirdPartyUserLoginBillsDtoCall;
import com.ymatou.userservice.service.WeiXinUserRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
*第三方用户管理
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SetThirdPartyUserLoginBillsDto{
	private static SetThirdPartyUserLoginBillsDtoBean setthirdpartyuserloginbillsdtoBean;
	private static SetThirdPartyUserLoginBillsDtoCall setthirdpartyuserloginbillsdtoCall;
	private static ULC201406Wapper userWapper;
	
	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("第三方用户管理接口");
		userWapper = new ULC201406Wapper();
	}

	@Before
	public void caseUp() {
		setthirdpartyuserloginbillsdtoBean=new SetThirdPartyUserLoginBillsDtoBean();
		setthirdpartyuserloginbillsdtoCall=new SetThirdPartyUserLoginBillsDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setthirdpartyuserloginbillsdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetThirdPartyUserLoginBillsDto_001() {
		Logger.start(true,"成功设置登陆邮箱，登陆密码");
		try {
			Logger.comment("新创建微信账号");
			Map user = regByWeiXin();
			int userId = ((Double)user.get("userId")).intValue();
			String email = TestDataManager.getEmail();
			Logger.comment("调用该接口设置邮箱信息和登录密码");
			setthirdpartyuserloginbillsdtoBean.setUserId(userId);
			setthirdpartyuserloginbillsdtoBean.setLoginEmail(email);
			setthirdpartyuserloginbillsdtoBean.setLoginPassword("123456");
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();			
			Logger.verifyEquals(ECode.SUCCESS, setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("ok", setthirdpartyuserloginbillsdtoCall.getString("Result"), "验证结果信息");
			
			ULC201406Bean ymtUser = userWapper.getULCInfoByUid(userId);
			Logger.verifyEquals(email.toLowerCase(), ymtUser.getlBills().getEmail(), "验证邮箱信息");
			Logger.verifyEquals("F47C010581713BD523BE9B43677C4872", ymtUser.getlPas(), "验证密码信息");
			Thread.sleep(3000);
			Logger.comment("验证登录");
			String ip = TestDataManager.getRandomIp();
			AuthCallService.authIPArea(ip, email, "123456", "MainSite");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(email);
			lab.setPas("123456");
			lab.setSourceType("MainSite");
			lac.setData(lab);
			lab.setClientIp(ip);
			lac.callService();
			Logger.verifyEquals("true", lac.getString("Success"), "验证登录成功");
			Logger.verifyEquals(email.toLowerCase(), lac.getUserData().getLoginMail(), "验证登陆邮箱");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetThirdPartyUserLoginBillsDto_002() {
		Logger.start(false,"登录邮箱已被非当前用户使用过，失败");
		try {
			Logger.comment("新创建微信账号");
			Map user = regByWeiXin();
			int userId = ((Double)user.get("userId")).intValue();
			setthirdpartyuserloginbillsdtoBean.setUserId(userId);
			setthirdpartyuserloginbillsdtoBean.setLoginEmail("rf4@gmail.com");
			setthirdpartyuserloginbillsdtoBean.setLoginPassword("123456");
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();	
			
			Logger.verifyEquals("403", setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("邮箱已被他用户使用", setthirdpartyuserloginbillsdtoCall.getString("LastErrorMessage"), "验证结果信息");
			Logger.verifyEquals("", setthirdpartyuserloginbillsdtoCall.getString("Result"), "验证结果为空");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetThirdPartyUserLoginBillsDto_003() {
		Logger.start(false,"登录邮箱格式错误，失败");
		try {
			Logger.comment("新创建微信账号");
			Map user = regByWeiXin();
			int userId = ((Double)user.get("userId")).intValue();
			setthirdpartyuserloginbillsdtoBean.setUserId(userId);
			setthirdpartyuserloginbillsdtoBean.setLoginEmail(TestDataManager.getRandomNum(7));
			setthirdpartyuserloginbillsdtoBean.setLoginPassword("123456");
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();	
			
			Logger.verifyEquals("401", setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("邮箱格式错误", setthirdpartyuserloginbillsdtoCall.getString("LastErrorMessage"), "验证结果信息");
			Logger.verifyEquals("", setthirdpartyuserloginbillsdtoCall.getString("Result"), "验证结果为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase//这个接口，输入的密码长度小于6，不符合要求时，返回结果不对
	public void Tc_SetThirdPartyUserLoginBillsDto_004() {
		Logger.start(true,"登录密码长度小于6，失败");
		try {
			Logger.comment("新创建微信账号");
			Map user = regByWeiXin();
			int userId = ((Double)user.get("userId")).intValue();
			setthirdpartyuserloginbillsdtoBean.setUserId(userId);
			setthirdpartyuserloginbillsdtoBean.setLoginEmail(TestDataManager.getEmail());
			setthirdpartyuserloginbillsdtoBean.setLoginPassword("456");
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();	
			
			Logger.verifyEquals("402", setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("登录密码长度小于6", setthirdpartyuserloginbillsdtoCall.getString("LastErrorMessage"), "验证结果信息");
			Logger.verifyEquals("", setthirdpartyuserloginbillsdtoCall.getString("Result"), "验证结果为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetThirdPartyUserLoginBillsDto_005() {
		Logger.start(false,"用户不存在，失败");
		try {
			setthirdpartyuserloginbillsdtoBean.setUserId(Integer.MAX_VALUE);
			setthirdpartyuserloginbillsdtoBean.setLoginEmail(TestDataManager.getEmail());
			setthirdpartyuserloginbillsdtoBean.setLoginPassword("456789");
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();	
						
			Logger.verifyEquals("404", setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("用户不存在", setthirdpartyuserloginbillsdtoCall.getString("LastErrorMessage"), "验证结果信息");
			Logger.verifyEquals("", setthirdpartyuserloginbillsdtoCall.getString("Result"), "验证结果为空");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetThirdPartyUserLoginBillsDto_006() {
		Logger.start(false,"未输入登陆密码，失败");
		try {
			Logger.comment("新创建微信账号");
			Map user = regByWeiXin();
			int userId = ((Double)user.get("userId")).intValue();
			String email = TestDataManager.getEmail();
			Logger.comment("调用该接口设置邮箱信息和登录密码");
			setthirdpartyuserloginbillsdtoBean.setUserId(userId);
			setthirdpartyuserloginbillsdtoBean.setLoginEmail(email);
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();			
			Logger.verifyEquals("402", setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("登录密码不能为空", setthirdpartyuserloginbillsdtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetThirdPartyUserLoginBillsDto_007() {
		Logger.start(false,"未输入邮箱，失败");
		try {
			Logger.comment("新创建微信账号");
			Map user = regByWeiXin();
			int userId = ((Double)user.get("userId")).intValue();
			Logger.comment("调用该接口设置邮箱信息和登录密码");
			setthirdpartyuserloginbillsdtoBean.setUserId(userId);
			setthirdpartyuserloginbillsdtoBean.setLoginPassword("123456");
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();					
			Logger.verifyEquals(ECode.ERROR400, setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("错误的请求参数", setthirdpartyuserloginbillsdtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("", setthirdpartyuserloginbillsdtoCall.getString("Result"), "验证结果信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetThirdPartyUserLoginBillsDto_008() {
		Logger.start(false,"未输入用户ID，失败");
		try {
			Logger.comment("新创建微信账号");
			String email = TestDataManager.getEmail();
			Logger.comment("调用该接口设置邮箱信息和登录密码");
			setthirdpartyuserloginbillsdtoBean.setLoginEmail(email);
			setthirdpartyuserloginbillsdtoBean.setLoginPassword("123456");
			setthirdpartyuserloginbillsdtoCall.setData(setthirdpartyuserloginbillsdtoBean);
			setthirdpartyuserloginbillsdtoCall.callService();					
			Logger.verifyEquals("404", setthirdpartyuserloginbillsdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setthirdpartyuserloginbillsdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("用户不存在", setthirdpartyuserloginbillsdtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("", setthirdpartyuserloginbillsdtoCall.getString("Result"), "验证结果信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 创建微信账号
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private Map regByWeiXin() throws ClientProtocolException, IOException{
		WeiXinUserRequestDtoBean regBean = new WeiXinUserRequestDtoBean();
		WeiXinUserRequestDtoCall regCall = new WeiXinUserRequestDtoCall();
		String loginId = "LoginWeiXinew_"+TestDataManager.getRandomNum(5);
		String openId = "RegopenId_"+TestDataManager.getRandomNum(7);
		regBean.setCountry("China");
		regBean.setProvince("Shanghai");
		regBean.setCity("Shanghai");
		regBean.setClientIp("1.1.1.1");
		regBean.setHeadimgurl("");
		regBean.setName(loginId);
		regBean.setOId(openId);
		regBean.setRegSource("mainsite");
		regBean.setSex("Femail");
		regBean.setMobile(TestDataManager.getPhoneNum());
		regBean.setTimeOut(new Date().getTime());
		regBean.setTSource("Pc");
		regBean.setUnionId("unionIdnew_"+TestDataManager.getRandomNum(10));
		regBean.setWeixinToken("weixintoken_"+TestDataManager.getRandomNum(10));
		
		regCall.setData(regBean);
		regCall.callService();	
		Logger.verifyEquals("注册成功", regCall.getString("SuccessMessage"), "验证注册成功");
		
		Map<String, Object> user = new HashMap<String, Object>();
		user.put("userId", regCall.getResult().get("UserId"));
		user.put("loginId", loginId);
		return user;
	}
}